<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>viewportclientposition_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_positioning_viewportclientposition_test.js.html">viewportclientposition_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.positioning.ViewportClientPositionTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.positioning.ViewportClientPositionTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.positioning.Corner&#39;);
<a name="line20"></a>goog.require(&#39;goog.positioning.Overflow&#39;);
<a name="line21"></a>goog.require(&#39;goog.positioning.ViewportClientPosition&#39;);
<a name="line22"></a>goog.require(&#39;goog.style&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line24"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line25"></a>
<a name="line26"></a>var viewportSize, anchor, popup, dom, frameRect;
<a name="line27"></a>var corner = goog.positioning.Corner;
<a name="line28"></a>
<a name="line29"></a>// Allow positions to be off by one in gecko as it reports scrolling
<a name="line30"></a>// offsets in steps of 2.
<a name="line31"></a>var ALLOWED_OFFSET = goog.userAgent.GECKO ? 1 : 0;
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>function setUp() {
<a name="line35"></a>  var frame = document.getElementById(&#39;frame1&#39;);
<a name="line36"></a>  var doc = goog.dom.getFrameContentDocument(frame);
<a name="line37"></a>
<a name="line38"></a>  dom = goog.dom.getDomHelper(doc);
<a name="line39"></a>  viewportSize = dom.getViewportSize();
<a name="line40"></a>  anchor = dom.getElement(&#39;anchor&#39;);
<a name="line41"></a>  popup = dom.getElement(&#39;popup&#39;);
<a name="line42"></a>  popup.style.overflowY = &#39;visible&#39;;
<a name="line43"></a>  goog.style.setSize(popup, 20, 20);
<a name="line44"></a>  frameRect = goog.style.getVisibleRectForElement(doc.body);
<a name="line45"></a>}
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>function testPositionAtCoordinateTopLeft() {
<a name="line49"></a>  var pos = new goog.positioning.ViewportClientPosition(100, 100);
<a name="line50"></a>  pos.reposition(popup, corner.TOP_LEFT);
<a name="line51"></a>
<a name="line52"></a>  var offset = goog.style.getPageOffset(popup);
<a name="line53"></a>  assertEquals(&#39;Left edge of popup should be at specified x coordinate.&#39;,
<a name="line54"></a>               100,
<a name="line55"></a>               offset.x);
<a name="line56"></a>  assertEquals(&#39;Top edge of popup should be at specified y coordinate.&#39;,
<a name="line57"></a>               100,
<a name="line58"></a>               offset.y);
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>function testPositionAtCoordinateBottomRight() {
<a name="line63"></a>  var pos = new goog.positioning.ViewportClientPosition(100, 100);
<a name="line64"></a>  pos.reposition(popup, corner.BOTTOM_RIGHT);
<a name="line65"></a>
<a name="line66"></a>  var bounds = goog.style.getBounds(popup);
<a name="line67"></a>  assertEquals(&#39;Right edge of popup should be at specified x coordinate.&#39;,
<a name="line68"></a>               100,
<a name="line69"></a>               bounds.left + bounds.width);
<a name="line70"></a>  assertEquals(&#39;Bottom edge of popup should be at specified x coordinate.&#39;,
<a name="line71"></a>               100,
<a name="line72"></a>               bounds.top + bounds.height);
<a name="line73"></a>}
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>function testPositionAtCoordinateTopLeftWithScroll() {
<a name="line77"></a>  dom.getDocument().body.style.paddingTop = &#39;300px&#39;;
<a name="line78"></a>  dom.getDocument().body.style.height = &#39;3000px&#39;;
<a name="line79"></a>  dom.getDocumentScrollElement().scrollTop = 50;
<a name="line80"></a>  dom.getDocument().body.scrollTop = 50;
<a name="line81"></a>
<a name="line82"></a>  var pos = new goog.positioning.ViewportClientPosition(0, 0);
<a name="line83"></a>  pos.reposition(popup, corner.TOP_LEFT);
<a name="line84"></a>
<a name="line85"></a>  var offset = goog.style.getPageOffset(popup);
<a name="line86"></a>  assertEquals(&#39;Left edge of popup should be at specified x coordinate.&#39;,
<a name="line87"></a>               0,
<a name="line88"></a>               offset.x);
<a name="line89"></a>  assertTrue(&#39;Top edge of popup should be at specified y coordinate &#39; +
<a name="line90"></a>             &#39;adjusted for scroll.&#39;,
<a name="line91"></a>             Math.abs(offset.y - 50) &lt;= ALLOWED_OFFSET);
<a name="line92"></a>
<a name="line93"></a>  dom.getDocument().body.style.paddingLeft = &#39;1000px&#39;;
<a name="line94"></a>  dom.getDocumentScrollElement().scrollLeft = 500;
<a name="line95"></a>
<a name="line96"></a>  pos.reposition(popup, corner.TOP_LEFT);
<a name="line97"></a>  offset = goog.style.getPageOffset(popup);
<a name="line98"></a>  assertTrue(&#39;Left edge of popup should be at specified x coordinate &#39; +
<a name="line99"></a>             &#39;adjusted for scroll.&#39;,
<a name="line100"></a>             Math.abs(offset.x - 500) &lt;= ALLOWED_OFFSET);
<a name="line101"></a>
<a name="line102"></a>  dom.getDocumentScrollElement().scrollLeft = 0;
<a name="line103"></a>  dom.getDocumentScrollElement().scrollTop = 0;
<a name="line104"></a>  dom.getDocument().body.style.paddingLeft = &#39;&#39;;
<a name="line105"></a>  dom.getDocument().body.style.paddingTop = &#39;&#39;;
<a name="line106"></a>
<a name="line107"></a>  pos.reposition(popup, corner.TOP_LEFT);
<a name="line108"></a>  offset = goog.style.getPageOffset(popup);
<a name="line109"></a>  assertEquals(&#39;Left edge of popup should be at specified x coordinate.&#39;,
<a name="line110"></a>               0,
<a name="line111"></a>               offset.x);
<a name="line112"></a>  assertEquals(&#39;Top edge of popup should be at specified y coordinate.&#39;,
<a name="line113"></a>               0,
<a name="line114"></a>               offset.y);
<a name="line115"></a>}
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>function testOverflowRightFlipHor() {
<a name="line119"></a>  var pos = new goog.positioning.ViewportClientPosition(frameRect.right,
<a name="line120"></a>                                                        100);
<a name="line121"></a>  pos.reposition(popup, corner.TOP_LEFT);
<a name="line122"></a>
<a name="line123"></a>  var offset = goog.style.getPageOffset(popup);
<a name="line124"></a>  assertEquals(&#39;Left edge of popup should have been adjusted so that it &#39; +
<a name="line125"></a>      &#39;fits inside the viewport.&#39;,
<a name="line126"></a>               frameRect.right - popup.offsetWidth,
<a name="line127"></a>               offset.x);
<a name="line128"></a>  assertEquals(&#39;Top edge of popup should be at specified y coordinate.&#39;,
<a name="line129"></a>               100,
<a name="line130"></a>               offset.y);
<a name="line131"></a>}
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>function testOverflowTopFlipVer() {
<a name="line135"></a>  var pos = new goog.positioning.ViewportClientPosition(100, 0);
<a name="line136"></a>  pos.reposition(popup, corner.TOP_RIGHT);
<a name="line137"></a>
<a name="line138"></a>  var offset = goog.style.getPageOffset(popup);
<a name="line139"></a>  assertEquals(&#39;Left edge of popup should be at specified x coordinate.&#39;,
<a name="line140"></a>               80,
<a name="line141"></a>               offset.x);
<a name="line142"></a>  assertEquals(&#39;Top edge of popup should have been adjusted so that it &#39; +
<a name="line143"></a>      &#39;fits inside the viewport.&#39;,
<a name="line144"></a>               0,
<a name="line145"></a>               offset.y);
<a name="line146"></a>}
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>function testOverflowBottomRightFlipBoth() {
<a name="line150"></a>  var pos = new goog.positioning.ViewportClientPosition(frameRect.right,
<a name="line151"></a>                                                        frameRect.bottom);
<a name="line152"></a>  pos.reposition(popup, corner.TOP_LEFT);
<a name="line153"></a>
<a name="line154"></a>  var offset = goog.style.getPageOffset(popup);
<a name="line155"></a>  assertEquals(&#39;Left edge of popup should have been adjusted so that it &#39; +
<a name="line156"></a>      &#39;fits inside the viewport.&#39;,
<a name="line157"></a>               frameRect.right - popup.offsetWidth,
<a name="line158"></a>               offset.x);
<a name="line159"></a>  assertEquals(&#39;Top edge of popup should have been adjusted so that it &#39; +
<a name="line160"></a>      &#39;fits inside the viewport.&#39;,
<a name="line161"></a>               frameRect.bottom - popup.offsetHeight,
<a name="line162"></a>               offset.y);
<a name="line163"></a>}
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>function testLastRespotOverflow() {
<a name="line167"></a>  var large = 2000;
<a name="line168"></a>  goog.style.setSize(popup, 20, large);
<a name="line169"></a>  popup.style.overflowY = &#39;auto&#39;;
<a name="line170"></a>
<a name="line171"></a>  var pos = new goog.positioning.ViewportClientPosition(0, 0);
<a name="line172"></a>  pos.reposition(popup, corner.TOP_LEFT);
<a name="line173"></a>
<a name="line174"></a>  assertEquals(large, popup.offsetHeight);
<a name="line175"></a>  pos.setLastResortOverflow(goog.positioning.Overflow.RESIZE_HEIGHT);
<a name="line176"></a>  pos.reposition(popup, corner.TOP_LEFT);
<a name="line177"></a>  assertNotEquals(large, popup.offsetHeight);
<a name="line178"></a>}
</pre>


</body>
</html>
